# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface cra
from pymongo import MongoClient


class JdPipeline:
    def __init__(self):
        self.number=0

        # 创建数据库链接对象
        #只要创建好mongodb的客户端和登录操作就可以直接插入数据了
        client = MongoClient('您的mongodb数据库地址',27017)#输入mongodb的ip地址和端口号
        self.db = client['admin']# 选择admin数据库
        self.db.authenticate('您的用户名','您的密码')#进行用户验证

    def process_item(self, item, spider):
        Item=dict(item)
        self.db.book.insert(Item)#往book集合里插入数据
        self.number+=1
        # print(Item)#书的详细信息
        print('第',self.number,'本书存入成功')
        return item
